package com.beiding.controller;

import com.beiding.exception.AuthorityException;
import com.beiding.exception.BadRequestException;
import com.beiding.exception.NotFoundException;
import com.beiding.exception.RegisterException;
import com.beiding.pojo.Error;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

@RestControllerAdvice
public class ExceptionHandlerController {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RegisterException.class)
    Error simple(RegisterException e) {
        return e.toError();
    }

    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    @ExceptionHandler(BindException.class)
    public List<FieldError> bind(BindException e)
    {
        return e.getFieldErrors();
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = BadRequestException.class)
    String badRequest(BadRequestException e) {
        return e.getMessage();
    }


    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(value = AuthorityException.class)
    String authority(AuthorityException e) {
        return e.getMessage();
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(value = NotFoundException.class)
    String notFound(NotFoundException e) {
        return e.getMessage();
    }

}